// 提现
const { setTitle, navBack, tipToast } = require('../../../common/env');
import { WITHDRAW_TYPE, WITHDRAW_TYPE_TEXT } from '../../../constants/common';
import { userApplyWithdraw, getUserPrivacyInfo } from '../../../api/usr';
Page({
    data: {
        name: '', // 实名
        idCard: '', // 身份证号
        withdrawType: WITHDRAW_TYPE.change,
        withdrawAmount: 0,
        typeEnmu: WITHDRAW_TYPE,
    },
    onLoad: async function (options) {
        const { type, amount } = options;
        setTitle(WITHDRAW_TYPE_TEXT[type]);
        const info = await getUserPrivacyInfo();
        this.setData({
            withdrawType: type,
            withdrawAmount: amount,
            name: info.realName || '',
            idCard: info.idCardNo || '',
        });
    },
    // 输入用户名修改
    onUserNameChange: function (event) {
        this.setData({
            name: event.detail,
        });
    },
    onUserIdCardChange: function (event) {
        this.setData({
            idCard: event.detail,
        })
    },
    // 确认提现
    ensureWithdraw: async function (event) {
        const amount = event.detail;
        const { idCard, name, withdrawType } = this.data;
        if (!name) {
            tipToast('收款人不能为空');
            return;
        }
        if (!idCard) {
            tipToast('身份证号不能为空');
            return;
        }
        const param = {
            target: withdrawType,
            amount,
            remark: WITHDRAW_TYPE_TEXT[withdrawType],
            realName: name,
            idCardNo: idCard,
        };
        // 执行提现操作
        const flag = await userApplyWithdraw(param);
        // 更新用户信息、收益数据
        if (flag) {
            // 返回收益界面
            navBack();
        } else {
            tipToast('提现失败，请重试');
        }
    },
})